package demo.bean;

import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 广州奥凯信息咨询有限公司
 * <p>
 * 本源代码由《项目一组》及其作者共同所有，未经版权持有者的事先书面授权，
 * 不得使用、复制、修改、合并、发布、分发和/或销售本源代码的副本。
 *
 * @copyright Copyright (广州奥凯信息咨询有限公司) 2019-2022. all rights reserved.
 */

public class DemoOfBeanInt {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "target\\class\\cglib");

        Enhancer enhancer = new Enhancer();
        enhancer.setInterfaces(new Class[]{Serializable.class});
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                return proxy.invokeSuper(obj, args);
            }
        });

        BeanGenerator beanGenerator = new BeanGenerator();
        beanGenerator.addProperty("id",Integer.class);

        Class o = (Class) beanGenerator.createClass();
        enhancer.setSuperclass(o);

        Object o1 = enhancer.create();
        Method method = o1.getClass().getMethod("setId", Integer.class);
        method.invoke(o1,100);

        Method getId = o1.getClass().getMethod("getId");
        Object invoke = getId.invoke(o1);
        System.out.println(invoke);
    }
}
